﻿//~~~~~~~~~~~~~~~~~~~~~~~~~
// CUSTOM TRIM() FUNCTION
//~~~~~~~~~~~~~~~~~~~~~~~~~
function trimout(str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK CAPLOCKS
//~~~~~~~~~~~~~~~~~~~~~~~~~

function capLock(e) {
    kc = e.keyCode ? e.keyCode : e.which;
    sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
    if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk))
        document.getElementById('divWarning').style.visibility = 'visible';
    else
        document.getElementById('divWarning').style.visibility = 'hidden';
}

$(document).ready(function () {
    $('.number').keydown(function () {
        
        if ((!(event.keyCode == 8                            // backspace
                    || event.keyCode == 46                              // delete
                    || event.keyCode == 9                               //tab
                    || (event.keyCode >= 35 && event.keyCode <= 40)     // arrow keys/home/end
                    || (event.keyCode >= 48 && event.keyCode <= 57)     // numbers on keyboard
                    || (event.keyCode >= 96 && event.keyCode <= 105))    // number on keypad
                || (event.shiftKey))   // shift used
        ) {
            event.returnValue = false; // Prevent character input
        }
    });

    setPageFilter = function (el) {
        var pf = el.split('#');
        var hidsearchid = $('#hidsearchid').val();
        var params = '{json:{"URL":"' + pf[0] + '","Key":' + pf[1] + ',"Params":"' + pf[2] + '", "Type":"' + pf[3] + '", "Source":"' + hidsearchid + '"}}';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/SetPageFilter",
            data: params,
            dataType: "json",
            success: function () {
                window.location = pf[0];
                try {
                    StopCheck();
                } catch (err) {
                    alert('*');
                }
            },
            error: function () {
                //alert('An error has occured retrieving data!');
            }
        });
        return false;
    };

    displayBreadcrumbs = function () {
        var breadcrumb;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/Recent",
            data: params,
            dataType: "json",
            success: function () {

                $('#BreadCrumbWrapper').html('hi');
            },
            error: function () {
                //alert('An error has occured retrieving data!');
            }
        });

    };

    clearSid = function () {
        //alert('hi');
        //$('#txtSid').val('');
    };

    formatCurrency = function (num) {
        num = isNaN(num) || num === '' || num === null ? 0.00 : num;
        return parseFloat(num).toFixed(2);
    };

    padnumber = function (num, digets) {
        var str = '' + num;
        while (str.length < digets) {
            str = '0' + str;
        }
        return str;
    };


    //a custom format option callback
    var addressFormatting = function (text) {
        var newText = text;
        //array of find replaces
        var findreps = [
            { find: /^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>' },
            { find: /([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>' },
            { find: /([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2' },
            { find: /([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>' },
            { find: /(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>' }
        ];

        for (var i in findreps) {
            newText = newText.replace(findreps[i].find, findreps[i].rep);
        }
        return newText;
    };

    FillDropdown = function (functionName) {
        var result;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/" + functionName,
            data: "{columnName:''}",
            async: false,
            dataType: "json",
            success: function (data, textStatus) {
                var msg = jQuery.parseJSON(data.d);
                if (msg.dropDownItem) {
                    result = msg.dropDownItem;
                    if (msg.errorMessage != '')
                        alert(msg.errorMessage);
                } 
            },
            error: function (data, textStatus) {
                result = false;
            }
        });
        return result;
    };

});